package cn.itsource.org.controller;


import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.DeaprtmentQuery;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    springMVC
        @GetMapping  =@RequestMapping( method = {RequestMethod.GET} )
        @PostMapping
        @PatchMapping
        @PutMapping
        @DeleteMapping

 */
@RestController //@Controller +@ResponseBody
@RequestMapping("/employee")
//@CrossOrigin//spring 5及以上的版本能用
public class EmployeeController {

    @Autowired
    private IEmployeeService service;

    @GetMapping
    public List<Employee> queryAll(){

        return  service.loadAll();
    }
   //添加和修改
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            service.saveOrUpdate(employee);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
     //分页查询
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        PageList<Employee> page = service.page(query);
        return  page;

    }
    //删除的
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }


}
